package com.lh.test.other;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

public class ActionAndNumber {
	public static void main(String[] args) {
		ArrayList<String> fileNames =  new ArrayList<String>();
		fileNames.add("winads.access_20130417.log");
		String name = "winads.access_201304172";
		for(int i=1;i<=3;i++){
			fileNames.add(name+i+".log");
		}
		name = "winads.access_201304180";
		for(int i=0;i<=9;i++){
			fileNames.add(name+i+".log");
		}
		fileNames.add("winads.access_2013041810.log");
		System.out.println("=========file name list==========");
		for(String s:fileNames){
			System.out.println(s);
		}
		System.out.println("=================================");
		ArrayList<Map<String, Integer>> arr = new ArrayList<Map<String, Integer>>();
		for(String fileName:fileNames){
			Map<String, Integer> map = readFileByLines(fileName);
			arr.add(map);
		}		
		Map<String, Long> totalMap = new HashMap<String, Long>();
		for (Map<String, Integer> map : arr) {
			for (Map.Entry<String, Integer> entry : map.entrySet()) {
				String key = entry.getKey();
				Integer value = entry.getValue();
				Long targetValue = totalMap.get(key);
				if (null == targetValue) {
					targetValue = new Long(value);
				} else {
					targetValue = targetValue + value;
				}
				totalMap.put(key, targetValue);
			}
		}
		System.out.println("=========所有的action汇总============");
		System.out.println("所有日志的action数为"+totalMap.size());
		System.out.println("所有日志的action详情");
		System.out.println(totalMap);
		System.out.println("=========所有的action汇总============");

	}

	/**
	 * 以行为单位读取文件，常用于读面向行的格式化文件
	 */
	public static Map<String, Integer> readFileByLines(String fileName) {
		Map<String, Integer> map = new HashMap<String, Integer>();
		InputStream in = ClassLoader.getSystemResourceAsStream(fileName);
		BufferedReader reader = null;
		try {
			System.out.println("读取文件"+fileName+"开始");
			reader = new BufferedReader(new InputStreamReader(in));
			String tempString = null;
			int line = 0;
			// 一次读入一行，直到读入null为文件结束
			while ((tempString = reader.readLine()) != null) {
				// 显示行号
				int sep = tempString.indexOf(".action?");
				line++;
				if (-1 == sep) {
					continue;
				} else {
					int tar = tempString.lastIndexOf('/', sep);
					if (-1 == tar) {
						continue;
					} else {
						String target = tempString.substring(tar + 1, sep);
						String key = target + ".action";
						Integer value = map.get(key);
						if (null == value) {
							map.put(key, 1);
						} else {
							map.put(key, value + 1);
						}
					}
				}
			}
			System.out.println(fileName+"文件一共有" + line+"行");			
		} catch (IOException e) {
			System.out.println(fileName+"文件读取出错");
			e.printStackTrace();
		} finally {
			if (reader != null) {
				try {
					reader.close();
				} catch (IOException e1) {
				}
			}
		}
		System.out.println(fileName+"文件里面有"+map.size()+"个不同的action名");
		System.out.println(fileName+"文件action具体详情"+map);
		System.out.println("读取文件"+fileName+"完成");
		return map;

	}

}
